package com.ouken.com;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gen.tool.AlertDialog_ok;
import com.gen.tool.SoundPlayer;
import com.ouken.asynTask.AsynAccountSave;
//import com.ouken.tool.MplayerTool;
import com.ouken.tool.MyApp;

public class AccountActivity extends Activity {
	private MyApp appState;
	private SharedPreferences userInfo;
	private	RelativeLayout RL_bg ;
	private	ImageButton ib_back;
	private	ImageButton ib_camera;
	private	ImageButton ib_album;
	private	ImageView iv_img;
	private	TextView tv_email ;
	private	EditText ev_nickName;
	private	ImageButton ib_save;
	private	EditText et_passwd ;
	private	EditText et_repasswd;
	private	EditText et_nickname;
	private	ImageView userimage;
	//private MplayerTool mplayerTool ;
	private Context context;
	
	
	
	private void initView(){
		userInfo  = getSharedPreferences("user_info", 0);
		appState = ((MyApp)getApplicationContext());
		//appState.setMusic(userInfo.getString("MUSIC", "ON").equals("ON"));
		appState.addActivity(AccountActivity.this);
		
		RL_bg = (RelativeLayout)findViewById(R.id.relativeLayout_account_bg);
	    ib_back = (ImageButton)findViewById(R.id.accountActivity_ib_back); 
	    ib_camera = (ImageButton)findViewById(R.id.accountActiviy_ib_camera);
	    ib_album = (ImageButton)findViewById(R.id.accountActiviy_ib_album);
        iv_img = (ImageView)findViewById(R.id.accountActiviy_iv_userimage);
        tv_email = (TextView)findViewById(R.id.accountActivity_tv_email);
        ev_nickName = (EditText)findViewById(R.id.accountActivity_et_nickname);
        ib_save = (ImageButton)findViewById(R.id.accountActivity_ib_save);
        
        et_passwd = (EditText)findViewById(R.id.accountActivity_et_passwd);
		et_repasswd = (EditText)findViewById(R.id.accountActivity_et_retypepasswd);
        et_nickname = (EditText)findViewById(R.id.accountActivity_et_nickname);
        userimage = (ImageView)findViewById(R.id.accountActiviy_iv_userimage);
		
        //mplayerTool = new MplayerTool(AccountActivity.this);
		
        context = AccountActivity.this;
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		SoundPlayer.pauseMusic();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SoundPlayer.startMusic();
	}

	public void onCreate(Bundle savedInstanceState) {
		   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account);
        initView();
		
        
        
        
        /*******************************user info process*******************************/
    	iv_img.setImageBitmap(appState.getImg());
    	tv_email.setText(appState.getUserEmail());
    	ev_nickName.setText(appState.getNickName());
        
       
	    
	    RL_bg.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				// TODO Auto-generated method stub
				((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(AccountActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		});
        
        ib_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//mplayerTool.playButtonPressed(AccountActivity.this);
				SoundPlayer.playSound(R.raw.keypress_common);
				AccountActivity.this.finish();
			}
		});
        
        ib_camera.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SoundPlayer.playSound(R.raw.keypress_common);
				//mplayerTool.playButtonPressed(AccountActivity.this);
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, 1);
			}
		});
        
        ib_album.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SoundPlayer.playSound(R.raw.keypress_common);
				//mplayerTool.playButtonPressed(AccountActivity.this);
				
				Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
			    intent.addCategory(Intent.CATEGORY_OPENABLE);
			    intent.setType("image/*");
			    intent.putExtra("crop", "true");
			    intent.putExtra("aspectX", 1);
			    intent.putExtra("aspectY", 1);
			    intent.putExtra("outputX", 80);
			    intent.putExtra("outputY", 80);
			    intent.putExtra("return-data", true);

			    startActivityForResult(intent, 1);
			}
		});
        
        ib_save.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SoundPlayer.playSound(R.raw.keypress_common);
				//mplayerTool.playButtonPressed(AccountActivity.this);
				
				
				
		        
		        userimage.setDrawingCacheEnabled(true);
		        Bitmap userimg_obmp = Bitmap.createBitmap(userimage.getDrawingCache());
		        userimage.setDrawingCacheEnabled(false);
		        
		        ByteArrayOutputStream out = new ByteArrayOutputStream();   
		        userimg_obmp.compress(Bitmap.CompressFormat.PNG, 100, out);
		        
		        
		        try {   
		            out.flush();   
		            out.close();   
		        } catch (IOException e) {   
		            e.printStackTrace();   
		        }  
		        
		        byte[] bb = out.toByteArray();
		        String test ="";
		        BigInteger bigInt = new BigInteger(1, bb);
		        test = bigInt.toString(16);
		        
		        String tmp = et_passwd.getText().toString();
		        et_passwd.setText(tmp.trim());
		        
		        tmp = et_repasswd.getText().toString();
		        et_repasswd.setText(tmp.trim());
		        
		        tmp = et_nickname.getText().toString();
		        et_nickname.setText(tmp.trim());
		        
		        
		        
		        
		        String checkResult = "OK";
		        String errorMessage = "" ;
		        if(!et_passwd.getText().toString().equals(et_repasswd.getText().toString())){
		        	checkResult = "ERROR";
		        	errorMessage += "twice passwords are not  the same!\n";
		        }
		        if(checkResult.equals("ERROR")){
			        AlertDialog_ok myDialog = new AlertDialog_ok(AccountActivity.this,errorMessage);
			        final AlertDialog now = myDialog.AlertDialog_ok_show();
					    
			        myDialog.getConfirm_btn_ok().setOnClickListener(new View.OnClickListener(){
						@Override  
						   public void onClick(View v) {  
								 now.dismiss(); 
							} 
						}
					);  	
					return;
					
		        }
		        
		       new  AsynAccountSave(context, appState,  et_passwd.getText().toString(),et_nickname.getText().toString(), test,userimg_obmp).execute();
		        
			}	
		});
	}
	@Override
	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		 
		 if(requestCode == 1){	
			if(data!=null){
				Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
				System.out.println("2");
				ImageView userimage = (ImageView)findViewById(R.id.accountActiviy_iv_userimage);
				userimage.setImageBitmap(cameraBitmap);
   	  }
     }
	  super.onActivityResult(requestCode, resultCode, data);

	 }

}
